import { defineStore } from "pinia";
import { ref } from "vue";
import router from "@/router";
//import { useRouter, useRoute } from 'vue-router';
import {
  getLoginUserUsingGet,
  userLogoutUsingPost,
} from "@/api/userController";
import ACCESS_ENUM from "@/access/accessEnum";

export const useLoginUserStore = defineStore(
  "loginUser",
  () => {
    const loginUser = ref<API.LoginUserVO>({
      userName: "未登录",
    });

    async function fetchLoginUser() {
      const res = await getLoginUserUsingGet();
      if (res.data.code === 0 && res.data.data) {
        loginUser.value = res.data.data;
      } else {
        loginUser.value = {
          userRole: ACCESS_ENUM.NOT_LOGIN,
          userName: "未登录",
        };
      }
    }
    async function logout() {
      const res = await userLogoutUsingPost();
      if (res.data.code === 0) {
        loginUser.value = {
          userRole: ACCESS_ENUM.NOT_LOGIN,
          userName: "未登录",
        };
        router.push('/user/login')
      }
    }

    function setLoginUser(newLoginUser: API.LoginUserVO) {
      loginUser.value = newLoginUser;
    }

    return { loginUser, setLoginUser, fetchLoginUser, logout };
  },
  {
    persist: true,
  }
);
